package com.zhengjiang.api.demo.controller;

import com.zhengjiang.api.demo.model.ApiResponse;
import com.zhengjiang.api.demo.model.AppInfo;
import com.zhengjiang.api.demo.utils.MD5Util;
import com.zhengjiang.api.demo.utils.RestTemplateUtil;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class BizController {

    /**
     * 模拟接口调用
     * @return
     */
    @GetMapping("getToken")
    public ApiResponse getToken(){
        String url = "http://localhost:8080/api/token/api_token";
        // 1. 根据appId查询数据库获取appSecret
        AppInfo appInfo = new AppInfo("1", "12345678954556");
        String timestamp = String.valueOf(System.currentTimeMillis());
        // 2. 计算签名
        String signString = appInfo.getAppId() + appInfo.getKey() + timestamp ;
        String signature = MD5Util.encode(signString);
        MultiValueMap<String,String> param = new LinkedMultiValueMap<>();
        param.add("appId",appInfo.getAppId());
        Map<String,String> headParam = new HashMap<>();
        headParam.put("timestamp",timestamp);
        headParam.put("sign",signature);
        return RestTemplateUtil.postForm(url,param,headParam,ApiResponse.class);
    }
}
